package com.my.geojson.test.swing;

import com.my.geojson.store.PostgisDataStore;
import com.my.geojson.test.simple.PostgisUtility;
import org.geotools.data.*;
import org.geotools.data.collection.SpatialIndexFeatureCollection;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.map.FeatureLayer;
import org.geotools.map.Layer;
import org.geotools.map.MapContent;
import org.geotools.styling.SLD;
import org.geotools.styling.Style;
import org.geotools.swing.JMapFrame;
import org.geotools.swing.data.JFileDataStoreChooser;
import org.opengis.feature.Property;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.referencing.crs.CoordinateReferenceSystem;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.Map;

/**
 * 图形化界面
 * 读取Shp信息
 * 同官方文档--快速开始
 */
public class SwingReadShpFile {
	
	public static void main(String[] args) throws IOException {
//		getInfoAndViewMap();
		inputPostGis();
	}

	/**
	 * 获取选择的文件的信息并展示出地图
	 * @throws IOException
	 */
	static void getInfoAndViewMap() throws IOException {
		//选择文件--会遇到直接运行无法弹出选择窗口的情况--使用run with coverage
		File file = JFileDataStoreChooser.showOpenFile("shp", null);
		if (file == null) {
			return;
		}

		FileDataStore store = FileDataStoreFinder.getDataStore(file);
		ServiceInfo info = store.getInfo();
		System.out.println(info);
		String typeName = store.getTypeNames()[0];
		System.out.println(typeName);

		SimpleFeatureType pgfeaturetype = store.getSchema();

		CoordinateReferenceSystem coordRef = pgfeaturetype.getGeometryDescriptor().getCoordinateReferenceSystem();
		System.out.println(coordRef.toString());

		Map<Object, Object> userDatas = pgfeaturetype.getGeometryDescriptor().getUserData();

		for(Map.Entry<Object, Object> entry : userDatas.entrySet()){
			Object mapKey = entry.getKey();
			Object mapValue = entry.getValue();
			System.out.println(mapKey+":"+mapValue);
		}
		//System.out.println(srid);

		SimpleFeatureSource featureSource = store.getFeatureSource();
		SimpleFeatureCollection simpleFeatureCollection = featureSource.getFeatures();
		//使用缓存---这里只是弹出地图的时候用了
		SimpleFeatureSource cachedSource =
				DataUtilities.source(
						new SpatialIndexFeatureCollection(featureSource.getFeatures()));

		System.out.println("Feature numbers in shape file:"+simpleFeatureCollection.size());

		SimpleFeatureIterator iterator = simpleFeatureCollection.features();
		while( iterator.hasNext() ){
			SimpleFeature feature = iterator.next();
			System.out.println(feature.getID() );
			Iterator<Property> props = feature.getProperties().iterator();
			while(props.hasNext()) {
				Property prop = props.next();
				System.out.println(prop.getName()+"=="+prop.getValue());
			}
			ListIterator<Object> attributes = feature.getAttributes().listIterator();
			while(attributes.hasNext()) {
				Object attribute = attributes.next();
				System.out.println(attribute);
			}
		}

		// Create a map content and add our shapefile to it
		MapContent map = new MapContent();
		map.setTitle("Quickstart");
		Style style = SLD.createSimpleStyle(featureSource.getSchema());
//        Layer layer = new FeatureLayer(featureSource, style);
		Layer layer = new FeatureLayer(cachedSource, style);
		map.addLayer(layer);
		// Now display the map显示地图
		JMapFrame.showMap(map);
	}

	/**
	 * 选择文件导入到数据库
	 */
	static void inputPostGis(){
		DataStore pgDataStore = PostgisDataStore.getInstance();
		File file = JFileDataStoreChooser.showOpenFile("shp", null);
		if (file == null) {
			return;
		}
		File pfile = file.getParentFile();

		FileFilter myFileFilter = new FileFilter(){

			@Override
			public boolean accept(File pathname) {
				String filename = pathname.getName().toLowerCase();
				if(filename.endsWith(".shp")){
					return true;
				}else{
					return false;
				}
			}
		};

		File[] fs = pfile.listFiles(myFileFilter);
		for(File f:fs){
			String fn= f.getPath();
			String tableName=fn.substring(fn.lastIndexOf(File.separator)+1,fn.lastIndexOf("."));
			System.out.println(f.getParent()+"///"+ tableName);
			try {
				PostgisUtility.importShp(f.getPath(), tableName);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		pgDataStore.dispose();
	}

}
